package fem3d;

import inf.text.ArrayFormat;

import java.io.IOException;

import fem2.Constraint;
import fem2.Debugger;
import fem2.Edge;
import fem2.Force;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.Model;
import fem2.Node;
import fem2.analysis.Analysis;
import fem2.analysis.LinearStaticAnalysis;
import fem2.element.TrussElement;
import fem2.enu.SolverType;
import fem2.enu.State;
import fem2.material.StVenantKirchhoffMM;
import fem2.pre_and_post.GidPostStructural;

/**
 * desmonstration of small tetraeder of oofem project
 * 
 * @author hbui
 * 
 */
public class SmallTetraeder {
	static String projDir = "/home/hbui/kratos_janosch";
	static String projName = "small-tetraeder.gid";

	public Model createModel() {
		Mesh mesh = new Mesh();
		double lb = 15.0;
		double r = 457.2 / 2000;
		double t = 10.0 / 1000;
		double a = Math.PI * (Math.pow(r, 2) - Math.pow(r - t, 2));
		double e = 2.1e11;
		Constraint c1 = new Constraint(false, false, false);
		Constraint c2 = new Constraint(true, true, false);

		/*
		 * create nodes
		 */
		Node n1 = mesh.addNode(new Node(0.0, 0.0, lb * Math.sqrt(2.0 / 3.0)));
		Node n2 = mesh.addNode(new Node(0.0, lb / Math.sqrt(3), 0));
		Node n3 = mesh.addNode(new Node(-lb / 2, -lb / Math.sqrt(12.0), 0));
		Node n4 = mesh.addNode(new Node(lb / 2, -lb / Math.sqrt(12.0), 0));

		/*
		 * create edges
		 */
		Edge e1 = mesh.addEdge(new Edge(n1, n2));
		Edge e2 = mesh.addEdge(new Edge(n1, n3));
		Edge e3 = mesh.addEdge(new Edge(n1, n4));
		Edge e4 = mesh.addEdge(new Edge(n2, n3));
		Edge e5 = mesh.addEdge(new Edge(n3, n4));
		Edge e6 = mesh.addEdge(new Edge(n4, n2));

		mesh.setNumberOfFields(3);

		n2.setConstraint(c1);
		n3.setConstraint(c1);
		n4.setConstraint(c2);

		Model m = new Model(mesh);
		Force f = new Force(n1, 0, -20e3, -100e3);
		m.addLoad(f);

		State ss = State.ONE_DIMENSIONAL;
		MaterialModel mm = new StVenantKirchhoffMM(e, 0.0, a, 0.0, ss);

		m.addElement(new TrussElement(e1, mm));
		m.addElement(new TrussElement(e2, mm));
		m.addElement(new TrussElement(e3, mm));
		m.addElement(new TrussElement(e4, mm));
		m.addElement(new TrussElement(e5, mm));
		m.addElement(new TrussElement(e6, mm));

		return m;
	}

	public static void main(String[] args) throws IOException {
		Model m = new SmallTetraeder().createModel();

		/*
		 * analysis
		 */
		Analysis an = new LinearStaticAnalysis(m);
		// ((NewtonRaphsonBasedStrategy) an.getStrategy())
		// .setMaxNewtonRaphsonIteration(1);
		an.getStrategy().setSolverType(SolverType.BAITSCH_SOLVER_GENERIC);
		// an.getStrategy().setEchoLevel(EchoLevel.OUTPUT, EchoLevel.DEBUG);
		an.run();

		/*
		 * post process
		 */
		Mesh mesh = m.getMesh();
		for (int i = 0; i < mesh.countNodes(); i++) {
			System.out.println(ArrayFormat.format(mesh.getNode(i).getUHatLocal()));
		}

		GidPostStructural post = new GidPostStructural(projDir, projName);
		post.clearAllPostFiles();
		post.writeMesh(m, 0);
		post.writeNodalDisplacementResults(m, 0);

		Debugger.warn("Analysis completed");

		// for (int i = 0; i < m.countElements(); i++) {
		// System.out.println(m.getElement(i).getID());
		// }
	}
}
